package com.settlement.system.service.impl;

import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.BrandMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.Brand;
import com.settlement.system.model.vo.TreeJson;
import com.settlement.system.service.IBrandService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.*;

/**
 * 品牌信息表 服务实现类
 * @author xiaochuan
 * @since 2020-04-13
 */
@Service
public class BrandServiceImpl extends BaseServiceImpl implements IBrandService {

    @Resource
    private BrandMapper brandMapper;

    @Override
    public BaseMapper init() {
        return brandMapper;
    }


    @Override
    public List<TreeJson> selectBrandWithBrandUnit(Map<String, Object> params) throws ServiceException {
        try {
            List<TreeJson> treeJsons = new ArrayList<>();
            //查询结果集
            List<Brand> tempList = brandMapper.selectBrandWithBrandUnit(params);

            TreeMap<String,TreeMap<String,Brand>> treeMap = new TreeMap<>();

            //品牌按品牌部分组
            for(Brand brand : tempList) {
                StringBuilder brandUnitKey = new StringBuilder();
                brandUnitKey.append(brand.getBrandUnitNo()).append(",").append(brand.getBrandUnitName());
                if(!treeMap.containsKey(brandUnitKey.toString())) {
                    //临时分组新增组
                    treeMap.put(brandUnitKey.toString(), new TreeMap<>());
                }
                treeMap.get(brandUnitKey.toString()).put(brand.getBrandNo(), brand);

            }
            //将临时分组内容添加至对应的返回结果集中
            for(Map.Entry<String,TreeMap<String,Brand>> entry:treeMap.entrySet()){
                TreeJson json = new TreeJson();
                String[] brandUnitKey = entry.getKey().split(",");
                String brandUnitNo =brandUnitKey[0];
                String brandUnitName =brandUnitKey[1];
                json.setId(brandUnitNo);
                json.setText(brandUnitName);
                List<TreeJson> childJsons = new ArrayList<>();
                for(Map.Entry<String,Brand> entry2:entry.getValue().entrySet()){
                    TreeJson json2 = new TreeJson();
                    json2.setId(entry2.getValue().getBrandNo());
                    json2.setText(entry2.getValue().getName());
                    childJsons.add(json2);
                }
                json.setChildren(childJsons);
                treeJsons.add(json);
            }
            return treeJsons;
        } catch (Exception e) {
            throw new ServiceException("获取品牌部插件异常", e);
        }
    }

    /**
     * 通过品牌名称获取品牌
     * @param brandName
     * @return
     * @throws ServiceException
     */
    @Override
    public List<Brand> getBrandByName(String brandName) throws ServiceException {
       try {
            return brandMapper.getBrandByName(brandName);
       } catch (Exception ex){
           throw new ServiceException("通过品牌名称获取品牌异常", ex);
       }
    }

    /**
     * 通过品牌编码获取品牌
     * @param brandNo
     * @return
     * @throws ServiceException
     */
    @Override
    public Brand getBrandByNo(String brandNo) throws ServiceException {
        try {
            return brandMapper.getBrandByNo(brandNo);
        } catch (Exception ex){
            throw new ServiceException("通过品牌编码获取品牌异常", ex);
        }
    }

}

